﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business;
using Entity;
using Entity.Custom;
using Newtonsoft.Json;
using Utilities;

namespace FreshCms.BackEnd.Handler
{
    /// <summary>
    /// Summary description for Log
    /// </summary>
    public class Log : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        #region Declare variable for check pms
        Int32 ui = 0;
        InPms chkPms = new InPms();
        InUser CurrentUser = new InUser();
        #endregion

        public void ProcessRequest(HttpContext context)
        {
            string m = "";
            var response = new ResponseData();

            #region Get PanelId and Check Psm
            CurrentUser = InUserBusiness.GetSessionInUser();
            if (!string.IsNullOrEmpty(context.Request["ui"]))
            {
                Int32.TryParse(context.Request["ui"], out ui);
                if (ui > 0)
                    chkPms = InPmsBusiness.CheckSessionPms(ui);
                if (chkPms == null)
                    chkPms = new InPms { PmsView = 0, PmsUpdate = 0, PmsDelete = 0 };
            }
            #endregion



            if (context.Request.RequestType == "POST")
            {
                if (context.Request["m"] != null)
                    m = context.Request["m"];
                else
                    return;
            }
            else
            {
                if (context.Request.QueryString["m"] != null)
                    m = context.Request.QueryString["m"];
                else
                    return;
            }

            switch (m.ToLower())
            {
                case "delete":
                    response = Delete(context);
                    break;
                case "deleteall":
                    response = DeleteAll(context);
                    break;
            }

            context.Response.ContentType = "application/x-javascript";

            string callBack = context.Request["callback"];
            if (!string.IsNullOrEmpty(callBack))
            {
                context.Response.Write(callBack + "( " + JsonConvert.SerializeObject(response) + " );");
            }
            else
                context.Response.Write(JsonConvert.SerializeObject(response));
        }

        /// <summary>
        /// Delete Log Checked
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private ResponseData Delete(HttpContext context)
        {
            // Check Pms
            if (chkPms.PmsDelete == 0)
                return new ResponseData { Data = null, ExtData = null, Message = Constant.PmsNoDelete, Success = false };

            var response = new ResponseData();
            if (!string.IsNullOrEmpty(context.Request["lst"]))
            {
                var lst = context.Request["lst"];

                response.Data = InLogBusiness.DeleteInLog(lst);
                response.Message = "Call Delete OK";
                response.Success = true;
                // log system 
                InLogBusiness.InsertInLog(new InLog
                {
                    UserId = CurrentUser.UserId,
                    LogAction = 0,
                    Field1 = "Xóa Log hệ thống thành công",
                    Field2 = "",
                    Field3 = "",
                    LogId = 0
                });
            }
            else
            {
                response.Data = -1;
                response.Message = "Param not valid";
                response.Success = false;
            }
            return response;
        }

        /// <summary>
        /// Delete Log Checked
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private ResponseData DeleteAll(HttpContext context)
        {
            // Check Pms
            if (chkPms.PmsDelete == 0)
                return new ResponseData { Data = null, ExtData = null, Message = Constant.PmsNoDelete, Success = false };

            var response = new ResponseData();
            
            response.Data = InLogBusiness.DeleteAllInLog();
            response.Message = "Call Delete OK";
            response.Success = true;
            // log system 
            InLogBusiness.InsertInLog(new InLog
            {
                UserId = CurrentUser.UserId,
                LogAction = 0,
                Field1 = "Xóa TOÀN BỘ Log hệ thống thành công",
                Field2 = "",
                Field3 = "",
                LogId = 0
            });
            return response;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}